package com.mmlc.meimei.lichengkongjian.classify;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.Adapter.SendAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.N4NoParamsRequestFilter;
import com.mmlc.network.responsebean.PrepareResponseBean;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/7.
 */
public class ModeSendSelectActivity  extends InterActivity{


    @Override
    protected int initContentView() {
        return R.layout.activity_mode_pay_select;
    }

    String companyName;

    ListView list_payment;
    SendAdapter adapter;
    TextView tv_title;
    int selectedMode = 0;
    List<PrepareResponseBean.DataBean.DeliveryListBean> list =  new ArrayList<>();

    @Override
    protected void initView() {
        selectedMode = getIntent().getIntExtra("mode", 0);

        list_payment = ViewUtils.getView(activity, R.id.list_payment);
        tv_title = ViewUtils.getView(activity, R.id.tv_title);
        tv_title.setText("配送方式");
        list_payment.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectedMode = position;
                adapter.setSelectedMode(selectedMode);
            }
        });
        ViewUtils.getView(activity, R.id.iv_back).setOnClickListener(this);
        ViewUtils.getView(activity, R.id.tv_verify).setOnClickListener(this);

        initInfo();
    }



    public void initInfo() {
        N4NoParamsRequestFilter filter = new N4NoParamsRequestFilter(activity, "prepare");
        filter.requestBean.zg_flag = 1;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<PrepareResponseBean>() {
            @Override
            public void onSuccess(PrepareResponseBean result) {
                if(result== null){
                    ToastUtils.showToast("数据解析错误");
                }
                list = new ArrayList<>();
                list = result.getData().getDelivery_list();
                adapter = new SendAdapter(ModeSendSelectActivity.this, list);
                list_payment.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                adapter.setSelectedMode(selectedMode);
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess);
            }
        });
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_verify:
                selectedMode = adapter.getSelectedMode();
                Intent intent = new Intent();
                intent.putExtra("mode", selectedMode);
                setResult(200, intent);
                finish();
                break;
        }
    }
}
